package com.example.myservlet;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.example.anno.RequestMapping;
import com.example.init.MinioUtil;
import com.google.common.net.HttpHeaders;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

@RequestMapping("/img")
public class ImageServlet extends HttpServlet {

    // 图片在磁盘上的绝对路径（换成你自己的）
    private static final String IMG_PATH = "C:/Users/wzp/Desktop/output_watermarked.jpg";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //展示磁盘文件
//        dispalyImgFile(req, resp);
//        qrcode(req, resp);
        minioFile(req, resp);
    }

    private void dispalyImgFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        File file = new File(IMG_PATH);
        if (!file.exists()) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found");
            return;
        }

        // 设置响应头：内容类型 + 长度
        resp.setContentType("image/jpeg");
        resp.setContentLengthLong(file.length());

        // 缓存控制（可选）
        resp.setHeader("Cache-Control", "max-age=86400");

        // 用 try-with-resources 把图片字节流原样写给客户端
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
             BufferedOutputStream out = new BufferedOutputStream(resp.getOutputStream())) {

            byte[] buffer = new byte[8192];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
        }
    }

    private void qrcode(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        // 生成到磁盘
        //使用hutool的QrCodeUtil需要包括
        //    <dependency>
        //      <groupId>com.google.zxing</groupId>
        //      <artifactId>core</artifactId>
        //      <version>3.5.1</version>
        //    </dependency>
        BufferedImage img = QrCodeUtil.generate("https://example.com", 300, 300);
        ImageIO.write(img, "png", resp.getOutputStream());
    }

    private void minioFile(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        InputStream inputStream = MinioUtil.downFile();
        resp.setContentType("image/jpeg"); // 设置MIME类型
//        resp.setHeader(HttpHeaders.CONTENT_DISPOSITION,
//                "attachment;filename=" + java.net.URLEncoder.encode(objectName, "UTF-8"));
        resp.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");

        // 3. 获取响应输出流
        OutputStream out = resp.getOutputStream();

        // 4. 流拷贝（直接传输）
        byte[] buffer = new byte[1024 * 4];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
    }
}